﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Common
{
    /// <summary>
    /// 软键盘调用类
    /// </summary>
    public class StartKeyBoard
    {
        /// <summary>
        /// 是否显示数字
        /// </summary>
        public static bool isShowNumBoard = false;

        /// <summary>
        /// 外部方法
        /// </summary>
        /// <param name="ptr"> </param>
        /// <returns> </returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

        /// <summary>
        /// 外部方法
        /// </summary>
        /// <param name="ptr"> </param>
        /// <returns> </returns>
        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

        /// <summary>
        /// 打开软键盘
        /// </summary>
        public static void StartKeyBoardFun()
        {
            //判断软键盘是否进程是否已经存在，如果不存在进行调用
            Process[] pro = Process.GetProcessesByName("osk");
            //说明已经存在，不再进行调用
            if (pro != null && pro.Length > 0)
                return;
            IntPtr ptr = new IntPtr();
            bool isWow64FsRedirectionDisabled = Wow64DisableWow64FsRedirection(ref ptr);
            //if (isWow64FsRedirectionDisabled)
            {
                Process.Start(@"C:\WINDOWS\system32\osk.exe");
                bool isWow64FsRedirectionReverted = Wow64RevertWow64FsRedirection(ptr);
            }
        }
    }
}